In [42]:
import random
alldata = []
all_words = "apple banana orange mango blueberry pizza cheese naruto".split()
for _ in range(5):
fruit = {}
for word in all_words:
n = random.randint(0,5) * 10
if n != 0:
fruit[word] = n
# print fruit
alldata.append(fruit)
alldata
Out[42]:
In [43]:
import pandas as pd
In [44]:
df = pd.DataFrame()
df['all'] = alldata
df
Out[44]:
In [45]:
def getCount(item, **kwargs):
column = kwargs['name']
if column in item:
return item[column]
else:
return 0
In [46]:
i = df.loc[0]['all']
i
Out[46]:
In [47]:
getCount(i, name='apple')
Out[47]:
In [48]:
df['all']
Out[48]:
In [49]:
cols = ['apple', 'banana']
for col in cols:
df[col] = df['all'].apply(getCount, name = col)
In [50]:
df
Out[50]:
In [ ]: